package cn.idietrade.idietradegoodsmodule8002.controller;

import cn.idietrade.common.pojo.SecondShoppingCart;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradegoodsmodule8002.service.carservice.SecondShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-21 11:47
 * @description
 */
@Slf4j
@RestController
public class SecondShoppingCartController {
    @Resource
    private SecondShoppingCartService secondShoppingCartService;

    Logger logger = Logger.getLogger("SecondShoppingCartController");

    @RequestMapping("/queryShoppingCarList")
    public ReturnResultData<List<SecondShoppingCart>> queryShoppingCarList(Integer userId){
        ReturnResultData<List<SecondShoppingCart>> resultData = new ReturnResultData<>();
        try {
            List<SecondShoppingCart> carts = secondShoppingCartService.queryShoppingCartUId(userId,null);
            if(EmptyUtils.isNotEmpty(carts)){
                resultData.setCode(1);
                resultData.setMessage("购物车查询成功");
                resultData.setData(carts);
            }else{
                resultData.setCode(0);
                resultData.setMessage("购物车查询失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            resultData.setData(null);
            logger.info("购物车查询----程序异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }

    @RequestMapping("/addShoppingGoods")
    public ReturnResultData addShoppingGoods(Integer userId,Integer goodsId){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int oneShoppingGoods = secondShoppingCartService.selectOneShoppingGoods(userId, goodsId);
            if(oneShoppingGoods==0){
                int flag = secondShoppingCartService.addShoppingGoods(userId, goodsId);
                if(flag>0){
                    List<SecondShoppingCart> carts = secondShoppingCartService.queryShoppingCartUId(userId, goodsId);
                    resultData.setCode(1);
                    resultData.setMessage("添加购物车成功");
                    resultData.setData(carts);
                }else{
                    resultData.setCode(0);
                    resultData.setMessage("添加购物车失败");
                    resultData.setData(null);
                }
            }else{
                int i = secondShoppingCartService.updateAddShoppingGoods(userId, goodsId);
                resultData.setCode(2);
                resultData.setMessage("添加购物车数量成功");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            resultData.setData(null);
            logger.info("添加购物车----程序异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }

    @RequestMapping("/deleShoppingGoods")
    public ReturnResultData deleShoppingGoods(Integer userId,Integer goodsId){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int flag = secondShoppingCartService.deleShoppingGoods(userId, goodsId);
            if(flag>0){
                resultData.setCode(1);
                resultData.setMessage("删除购物车成功");
                resultData.setData(null);
            }else{
                resultData.setCode(0);
                resultData.setMessage("删除购物车失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            resultData.setData(null);
            logger.info("删除购物车----程序异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }

    @RequestMapping("/updateShoppingGoods")
    public ReturnResultData updateShoppingGoods(Integer userId,Integer goodsId,Integer num){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int flag = secondShoppingCartService.updateShoppingCart(userId,goodsId,num);
            if(flag>0){
                resultData.setCode(1);
                resultData.setMessage("修改购物车数量成功");
                resultData.setData(null);
            }else{
                resultData.setCode(0);
                resultData.setMessage("修改购物车数量失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            resultData.setData(null);
            logger.info("修改购物车数量----程序异常");
            logger.info(e.getMessage());
            e.printStackTrace();
        }
        return resultData;
    }

}
